package com.mspbots.rule.exceptions.attendance;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mspbots.api.service.TenantUserService;
import com.mspbots.cw.request.AttendanceDTO;
import com.mspbots.cw.request.TenantUserConfigurationDTO;
import com.mspbots.rule.exceptions.BaseRule;
import lombok.extern.slf4j.Slf4j;
import org.jeasy.rules.annotation.Action;
import org.jeasy.rules.annotation.Fact;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Map;


/**
 * @ClassName com.mspbots.rule.exceptions.attendance.AttendanceBaseRule
 * @Description TODO
 * @Author Jun
 * @Date 2019/9/24 15:20
 * @Version 1.0
 **/
@Slf4j
@Component
public abstract class AttendanceBaseRule extends BaseRule {

    protected int tz;
    protected String tzStr = "";
    protected TenantUserService tenantUserService;


    public AttendanceBaseRule() {
    }

    public void initWorkConfig(TenantUserConfigurationDTO tenantUserConfigurationDTO) {
        this.userWorkTimeConfig = JSONUtil.parseObj(tenantUserConfigurationDTO.getKeywordValue());
        tz = Integer.parseInt(userWorkTimeConfig.getStr("offset").substring(0, 3));
        tzStr = userWorkTimeConfig.getStr("timezone");
    }

    protected boolean in12Hour(AttendanceDTO attendanceDTO) {
        return LocalDateTime.now().minusHours(12).isBefore(attendanceDTO.getLogTime().minusHours(tz));
    }

    protected LocalDateTime logTimeUTC(AttendanceDTO attendanceDTO) {
        return attendanceDTO.getLogTime().minusHours(tz);
    }

    protected AttendanceDTO getAttendance(Map<String, Object> params) {
        return this.coreClient.get("/attendances/last", params, AttendanceDTO.class).block();
    }


    @Override
    protected String buildMessage(LocalDateTime localDateTime) {
        return new StringBuilder()
                .append(formatMessage(true))
                .append("--")
                .append("<span style='color:#999d9c;font-size:10px;'>")
                .append(suffixUserName())
                .append(suffixAttendanceZoneTime(localDateTime))
                .append("</span>").toString();
    }

    protected boolean insertMessage() {
        messages.setBusinessType("attendance");
        return super.insertMessage(messages);
    }

    protected LocalTime getTodayTime(String type) {
        try {
            DayOfWeek dayOfWeek = LocalDateTime.now().plusHours(tz).getDayOfWeek();
            JSONArray workTimeArr = userWorkTimeConfig.get("workTimes", JSONArray.class);
            JSONObject workTime = (JSONObject) workTimeArr.get(dayOfWeek.getValue() - 1);
            String startTimeStr = workTime.getStr(type);
            return StringUtils.isEmpty(startTimeStr) ? null : LocalTime.parse(startTimeStr + ":00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @Action
    public void action(@Fact(FACT) TenantUserConfigurationDTO tenantUserConfigurationDTO) {
        String message = buildMessage(LocalDateTime.now());
        if (message != null) {
            messages.setMessage(message);
            messages.setBusinessId(tenantUserConfigurationDTO.getId());
            insertMessage();
        }


    }


    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    public void setTenantUserService(TenantUserService tenantUserService) {
        this.tenantUserService = tenantUserService;
    }

    @Override
    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }


}
